package com.util;

import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class DBHelper {
	private static SqlSessionFactory sessionFactory = null;
	//test
	public SqlSession session() throws Exception{
		String resource = "MyBatisConfig.xml";
		
		Reader reader = Resources.getResourceAsReader(resource);
		SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
		SqlSessionFactory factory = builder.build(reader);
		SqlSession session = factory.openSession();
		return session;
	}
	
	protected SqlSession getSession() {
		return sessionFactory.openSession();
	}

	protected void close(SqlSession session) {
		if (session != null) {
			session.close();
			session = null;
		}
	}

}
